#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
#include <string>
using namespace std;

#define rep(i,n) for (int i=0; i<n; i++)

#define TRACE(x...) 
#define PRINT(x...) TRACE(printf(x))

#define MULT_TEST(x...)  

int _42;
int n, m;
int prices[110];
int qtd[110];
char tmp[40];
int cont;
map<string, int> inds;
int sum;

int main() {
	MULT_TEST(
		scanf("%d", &_42);

		while (_42--) {
	)

	inds.clear();
	cont=0;
	scanf("%d %d", &n, &m);

	rep(i,n) scanf("%d", &prices[i]);
	rep(i,n) qtd[i]=0;

	rep(i,m) {
		scanf(" %s", tmp); 
		if (inds.find(tmp) == inds.end())
			inds[tmp]=cont++;

		qtd[inds[tmp]]++;	
	}

	sort(prices, prices+n);
	sort(qtd, qtd+cont);

	TRACE(
		rep(i,n) printf("%d ", prices[i]);
		printf("\n");
		rep(i,cont) printf("%d ", qtd[i]);
		printf("\n");
	)

	sum=0;

	rep(i,cont) {
		sum+=(qtd[cont-i-1]*prices[i]);
		PRINT("%d %d\n", qtd[cont-i-1], prices[i]);
	}

	
	printf("%d ", sum);
	PRINT("\n");
	sum=0;

	rep(i,cont) {
		sum+=(qtd[cont-i-1]*prices[n-i-1]);
		PRINT("%d %d\n", qtd[i], prices[n-i-1]);
	}

	printf("%d\n", sum);
	MULT_TEST( } )
	
	return 0;
}
